﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HA.Data;
namespace HA.ModFlow.Controls.PresentationClass
{
    public class PCMt3dGridCellProperty
    {
        private double _icbund;

        public double Icbund
        {
            get { return _icbund; }
            set { _icbund = value; }
        }
        private double _htop;

        public double Htop
        {
            get { return _htop; }
            set { _htop = value; }
        }
        private double _porosity;

        public double Porosity
        {
            get { return _porosity; }
            set { _porosity = value; }
        }
        private double _longDispersivity;

        public double LongDispersivity
        {
            get { return _longDispersivity; }
            set { _longDispersivity = value; }
        }
        private double _bulkDensity;

        public double BulkDensity
        {
            get { return _bulkDensity; }
            set { _bulkDensity = value; }
        }
        private double _immobilePorosity;

        public double ImmobilePorosity
        {
            get { return _immobilePorosity; }
            set { _immobilePorosity = value; }
        }

        List<Species> _arrySpecies = new List<Species>();

        internal List<Species> ArrySpecies
        {
            get { return _arrySpecies; }
            set { _arrySpecies = value; }
        }


        public void InitializationData(Project proj, List<int> cellIndex)
        {
            if (proj.Mt3dmsData == null)
            {
                return;
            }

            HA.ModFlow.Infrastructure.CellCenterDataSet3d cell;
            int layer = proj.Mt3dmsData.BasicTransport.Grid.Nlayers;
            int row = proj.Mt3dmsData.BasicTransport.Grid.Nrows;
            int col = proj.Mt3dmsData.BasicTransport.Grid.Ncols;

            int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[0]);
            int selectCellRow = ijk[0] - 1;
            int selectCellCol = ijk[1] - 1;
            int selectCellLayer = ijk[2] - 1;

            cell = proj.Mt3dmsData.BasicTransport.Grid.GetICBoundDS();
            this.Icbund = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.BasicTransport.Grid.Elevations, proj.Mt3dmsData.BasicTransport.Grid.GetICBUND(), row, col, layer);
            this.Htop = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.BasicTransport.Grid.GetPorosity(), proj.Mt3dmsData.BasicTransport.Grid.GetICBUND(), row, col, layer);
            this.Porosity = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.Dispersion.AL, null, row, col, layer);
            this.LongDispersivity = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.ChemicalReaction.BulkDensity, null, row, col, layer);
            this.BulkDensity = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.ChemicalReaction.Porosity2, null, row, col, layer);
            this.ImmobilePorosity = cell.Datas[selectCellRow, selectCellCol, selectCellLayer];


            for (int i = 0; i < proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies.Count; i++)
            {
                Species sp = new Species();
                sp.Name = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].SpName;
                sp.IC = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].SrcConc[selectCellLayer][selectCellRow, selectCellCol];
                sp.Const1 = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Sp1[selectCellLayer][selectCellRow, selectCellCol];
                sp.Const2 = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Sp2[selectCellLayer][selectCellRow, selectCellCol];
                sp.Rate1 = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Rc1[selectCellLayer][selectCellRow, selectCellCol];
                sp.Rate2 = proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Rc2[selectCellLayer][selectCellRow, selectCellCol];              
                this.ArrySpecies.Add(sp);
            }
        }
        public void SetData(Project proj, List<int> cellIndex)
        {
            if (proj.Mt3dmsData == null)
            {
                return;
            }
            
            int layer = proj.Mt3dmsData.BasicTransport.Grid.Nlayers;
            int row = proj.Mt3dmsData.BasicTransport.Grid.Nrows;
            int col = proj.Mt3dmsData.BasicTransport.Grid.Ncols;
            HA.ModFlow.Infrastructure.CellCenterDataSet3d cell;

            cell = proj.Mt3dmsData.BasicTransport.Grid.GetICBoundDS();
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.Icbund;
            }
            proj.Mt3dmsData.BasicTransport.Grid.SetICBoundDS(cell);

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.BasicTransport.Grid.Elevations, proj.Mt3dmsData.BasicTransport.Grid.GetICBUND(), row, col, layer);
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.Htop;
            }
            proj.Mt3dmsData.BasicTransport.Grid.SetTopH(cell.ToListData());

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.BasicTransport.Grid.Elevations, proj.Mt3dmsData.BasicTransport.Grid.GetICBUND(), row, col, layer);
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.Porosity;
            }
            proj.Mt3dmsData.BasicTransport.Grid.SetPorosity(cell.ToListData());


            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.Dispersion.AL, null, row, col, layer);
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.LongDispersivity;
            }
            proj.Mt3dmsData.Dispersion.SetAL(cell.ToListData());

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.ChemicalReaction.BulkDensity, null, row, col, layer);
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.BulkDensity;
            }
            proj.Mt3dmsData.ChemicalReaction.SetBulkDensity(cell.ToListData());

            cell = new Infrastructure.CellCenterDataSet3d(proj.Mt3dmsData.ChemicalReaction.Porosity2, null, row, col, layer);
            for (int i = 0; i < cellIndex.Count; i++)
            {
                int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[i]);
                int selectCellRow = ijk[0] - 1;
                int selectCellCol = ijk[1] - 1;
                int selectCellLayer = ijk[2] - 1;

                cell.Datas[selectCellRow, selectCellCol, selectCellLayer] = this.ImmobilePorosity;
            }
            proj.Mt3dmsData.ChemicalReaction.SetPorosity2(cell.ToListData());



            for (int i = 0; i < proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies.Count; i++)
            {
                for (int j = 0; j < cellIndex.Count; j++)
                {
                    int[] ijk = proj.Grid3dData.MainGrid.ModFlowGrid.GetIJK(cellIndex[j]);
                    int selectCellRow = ijk[0] - 1;
                    int selectCellCol = ijk[1] - 1;
                    int selectCellLayer = ijk[2] - 1;


                   proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].SrcConc[selectCellLayer][selectCellRow, selectCellCol]= this.ArrySpecies[i].IC  ;
                   proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Sp1[selectCellLayer][selectCellRow, selectCellCol] = this.ArrySpecies[i].Const1;
                   proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Sp2[selectCellLayer][selectCellRow, selectCellCol] = this.ArrySpecies[i].Const2;
                   proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Rc1[selectCellLayer][selectCellRow, selectCellCol] = this.ArrySpecies[i].Rate1;
                   proj.Mt3dmsData.ChemicalReaction.ChemicalSpecies[i].Rc2[selectCellLayer][selectCellRow, selectCellCol] = this.ArrySpecies[i].Rate2;
                }
            }


        }
    }

    class Species
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private double _StartConc;

        private double _iC;

        public double IC
        {
            get { return _iC; }
            set { _iC = value; }
        }
        private double _const1;

        public double Const1
        {
            get { return _const1; }
            set { _const1 = value; }
        }
        private double _const2;

        public double Const2
        {
            get { return _const2; }
            set { _const2 = value; }
        }
        private double _rate1;

        public double Rate1
        {
            get { return _rate1; }
            set { _rate1 = value; }
        }
        private double _rate2;

        public double Rate2
        {
            get { return _rate2; }
            set { _rate2 = value; }
        }
    }

}
